import type { Job, JobStatus } from '@/apis/model/job.ts'
import type { ResourceItem } from './types'
import { E_JOB_TYPE } from '@/enums/job'
import { formatBandwidth, formatEmptyValue } from '@/utils/format.ts'

export default function useResourceList(orderTask: Ref<Job>) {
  const data = ref<ResourceItem[]>([])

  const order = computed(() => orderTask.value.job_info!)
  const orderType = computed(() => Number(order.value.job_type) as E_JOB_TYPE)
  const productType = computed(
    () => Number(order.value.job_product) as E_JOB_PRODUCT,
  )
  let resourceStatus: JobStatus = {}
  let currentIndex = 0

  /**
   * 处理vps套餐数据
   */
  function _handleVpsPackage() {
    const vps = order.value.job_data!.compute_job!
    const instanceId = vps.id![currentIndex]
    data.value.push({
      name: '云服务器',
      key: 'vps',
      value: formatEmptyValue(vps.group),
      status: resourceStatus.instance_status!,
      reason: resourceStatus.instance_failed_reason,
    })
    data.value.push({
      name: '云硬盘',
      key: 'disk',
      value: `${vps
        .disk!.filter(item => item.instance_id === instanceId).reduce((a, b) => {
        return a + b.size!
      }, 0)} GB`,
      status: resourceStatus.disk_status!,
      reason: resourceStatus.disk_failed_reason,
    })
    data.value.push({
      name: '公网IP',
      key: 'ip',
      value: `${vps.ip!.filter(item => item.instance_id === instanceId).length} 个`,
      status: resourceStatus.ip_status!,
      reason: resourceStatus.ip_failed_reason,
    })
    data.value.push({
      name: '带宽',
      key: 'bandwidth',
      value: formatBandwidth(vps.ip?.[currentIndex].bandwidth),
      status: resourceStatus.ip_status!,
      reason: resourceStatus.ip_failed_reason,
    })
  }

  /**
   * 处理裸金属套餐数据
   */
  function _handleBareMetalPackage() {
    const bareMetal = order.value.job_data!.bare_metal_job!
    const instanceId = bareMetal.id![currentIndex]
    data.value.push({
      name: '裸金属',
      key: 'bare-metal',
      value: formatEmptyValue(bareMetal.group),
      status: resourceStatus.instance_status!,
      reason: resourceStatus.instance_failed_reason,
    })
    data.value.push({
      name: '公网 IP',
      key: 'ip',
      value: `${bareMetal.create_ip?.filter(item => item.instance_id === instanceId).length} 个`,
      status: resourceStatus.ip_status!,
      reason: resourceStatus.ip_failed_reason,
    })
    data.value.push({
      name: '带宽',
      key: 'bandwidth',
      value: formatBandwidth(bareMetal.create_ip?.[currentIndex].bandwidth),
      status: resourceStatus.ip_status!,
      reason: resourceStatus.ip_failed_reason,
    })
  }

  /**
   * 处理云硬盘数据
   */
  function _handleDisk() {
    const disk = order.value.job_data!.disk_job!
    data.value.push({
      name: '云硬盘',
      key: 'disk',
      value: disk.disk!.map((item) => {
        return {
          label: item.type === 1 ? '系统盘' : '数据盘',
          value: `${item.size} GB`,
        }
      }),
      status: resourceStatus.disk_status!,
      reason: resourceStatus.disk_failed_reason,
    })
  }

  /**
   * 处理快照数据
   */
  function _handleSnapshot() {
    const snapshot = order.value.job_data!.snapshot_job!
    data.value.push({
      name: '快照',
      key: 'snapshot',
      value: `${snapshot.disk_size} GB`,
      status: resourceStatus.snapshot_status!,
      reason: resourceStatus.snapshot_failed_reason,
    })
  }

  /**
   * 处理ip带宽数据
   */
  function _handleIpBandwidth() {
    // const network = order.value.job_data!.ip_job!
    // const bandwidth = formatBandwidth(network.bandwidth)
    data.value.push({
      name: '公网IP',
      key: 'ip',
      value: '-',
      status: resourceStatus.ip_status!,
      reason: resourceStatus.ip_failed_reason,
    })
  }

  /**
   * 初始化数据
   */
  function init() {
    if (!order.value) {
      return
    }

    data.value = []
    orderTask.value.status?.forEach((item, index) => {
      currentIndex = index
      resourceStatus = item
      if (orderType.value === E_JOB_TYPE.CREATE_VPS) {
        _handleVpsPackage()
      }
      if (orderType.value === E_JOB_TYPE.CREATE_BARE_METAL) {
        _handleBareMetalPackage()
      }
      if (orderType.value === E_JOB_TYPE.CREATE_DISK) {
        _handleDisk()
      }
      if (orderType.value === E_JOB_TYPE.CREATE_SNAPSHOT) {
        _handleSnapshot()
      }
      if (
        orderType.value === E_JOB_TYPE.CREATE_IP
        || orderType.value === E_JOB_TYPE.CHANGE_IP
        || orderType.value === E_JOB_TYPE.UPDATE_BANDWIDTH
      ) {
        _handleIpBandwidth()
      }
    })
  }

  watch(orderTask, init, { deep: true, immediate: true })

  return data
}
